package com.naizhuai.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.naizhuai.R;
import com.naizhuai.base.DialogControl;
import com.naizhuai.ui.components.TipDialog;
import com.naizhuai.util.SharedPreferenceUtil;
import com.naizhuai.util.ToastCompat;

import java.util.concurrent.atomic.AtomicInteger;

import rx.Subscription;

/**
 * @author：lichenxi
 * @date 2018/2/23 17
 * email：525603977@qq.com
 * Fighting
 */
public  class BaseFragment extends Fragment {
    private SharedPreferenceUtil mSharedPreferenceUtil;
    private BaseActivity mBaseActivity;
    private  static  AtomicInteger mAtomicInteger=new AtomicInteger(3);
    public SharedPreferenceUtil getSp() {
        if(mSharedPreferenceUtil==null){
            mSharedPreferenceUtil=SharedPreferenceUtil.getInstance(getActivity());
        }
        return mSharedPreferenceUtil;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public BaseActivity getBaseActivity() {
        return mBaseActivity;
    }

    protected void longToast(String str){
        ToastCompat.showToast(getActivity(),str,Toast.LENGTH_LONG);
    }

    protected void shortToast(String str){
        ToastCompat.showToast(getActivity(),str,Toast.LENGTH_SHORT);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        mBaseActivity.overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mBaseActivity=(BaseActivity) context;
    }


    public TipDialog showWaitDiaLog() {
        return showWaitDialog(getString(R.string.loading));
    }

    public TipDialog showWaitDialog(@NonNull int resid) {
        return showWaitDialog(getString(resid));
    }

    public TipDialog showWaitDialog(@NonNull String string) {
        if (mBaseActivity instanceof DialogControl){
           return mBaseActivity.showWaitDialog(string);
        }
        return null;
    }

    public void hideWaitDialog() {
          mBaseActivity.hideWaitDialog();
    }

    public TipDialog showWaitDiaLog(Subscription subscription) {
        return mBaseActivity.showWaitDiaLog(subscription);
    }

    public void incrementcount(){
        mAtomicInteger.getAndIncrement();

    }
    public void decrementcount(){
        mAtomicInteger.getAndDecrement();
    }
    public int getcount(){
       return mAtomicInteger.get();
    }

    public void reset(){
         mAtomicInteger.set(3);
    }
}
